<?php
namespace English\Controller;
use Think\Controller;
class SecurityCodeController extends Controller {
	private $cacheTime;
	private $d_content,$d_category;	
	public function __construct()
	{
		parent::__construct();
	}
	//首页
    public function index(){
		$html_info=S('security_code_index');
		if($html_info)die($html_info);
		$this->_init();
		//获取
		$seo=$GLOBALS['setting_model']->getSetting(4);
		foreach ($seo as $key => $value) {
			$seo[$key] = '防伪码查询-'.$value;
		}
		$this->assign($seo);	
		$html_info=$this->fetch('SecurityCode/index');
		S('security_code_index',$html_info,$this->cacheTime);
		echo $html_info;
    }
	public function step2()
	{
		//验证验证码是否正确
        if(empty($_POST['code']))
        {
            $this->error("验证码不能为空！");
			exit;	 
        }
		$verify = new \Think\Verify();
        if(!$verify->check($_POST['code']))
		{
			$this->error("验证码有误！");
			exit;	
		}			
		$code = empty($_POST['input_code'])?'':$_POST['input_code'];
		$security_code = M('security_code')->where("product_sn='$code'")->find();	
		$html_info=S('security_code_step2');
		if($html_info)die($html_info);
		$this->_init();		
		//获取
		$seo=$GLOBALS['setting_model']->getSetting(4);
		foreach ($seo as $key => $value) {
			$seo[$key] = '防伪码查询-'.$value;
		}
		$this->assign($seo);			
		if (empty($security_code)){
			$info = "您输入的防伪码出错";
		}else{
			$data['num'] = $security_code['num']+1 ;
			//$data['status'] =1;
			$data['use_time'] = time();
			$result=M('security_code')->where('id='.$security_code['id'])->data($data)->filter('new_html_special_chars')->save();
			if($result!==false){
				$info = '您的产品是正品，请放心使用！';
			}else{
				$info = '查询出错！';
			}
		}
		$this->assign('info',$info);
		$html_info=$this->fetch('SecurityCode/step2');
		S('security_code_step2',$html_info,$this->cacheTime);
		echo $html_info;
	}  
	public function qr()
	{
		$code = empty($_GET['input_code'])?'':$_GET['input_code'];
		$security_code = M('security_code')->where("product_sn='$code'")->find();	
		$html_info=S('security_code_qr');
		if($html_info)die($html_info);
		$this->_init();		
		//获取
		$seo=$GLOBALS['setting_model']->getSetting(4);
		foreach ($seo as $key => $value) {
			$seo[$key] = '防伪码查询-'.$value;
		}
		$this->assign($seo);			
		if (empty($code)){
			$info = "您的产品是不是正品，请注意！";
		}else{
			$data['num'] = $security_code['num']+1 ;
			$data['status'] =1;
			$data['use_time'] = time();
			$result=M('security_code')->where('id='.$security_code['id'])->data($data)->filter('new_html_special_chars')->save();
			if($result!==false){
				$info = '您的产品是正品，请放心使用！';
			}else{
				$info = '查询出错！';
			}
		}
		$this->assign('info',$info);
		$html_info=$this->fetch('SecurityCode/qr');
		S('security_code_qr',$html_info,$this->cacheTime);
		echo $html_info;
	}   	  
	//初始化网站
	private function _init()
	{
		$site_info=$GLOBALS['setting_model']->getSetting(5);
		$this->cacheTime=$site_info['cachetime'];
		$this->assign('siteinfo',$site_info);
		if(!$this->d_content)
		{
			$this->d_content=D('Content/content');
			$this->assign('d_content',$this->d_content);			
		}
		if(!$this->d_category)
		{
			$this->d_category=D('Content/category');
			$this->assign('d_category',$this->d_category);			
		}
		$navs=M('content_nav')->where('status=1')->select();	
		$this->assign('navs',$navs);
		$this->cacheTime=intval(C('cacheTime'));	
	}
}
adasdasd22222222222222;